//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LmLinearExponentialCorrelationModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
#include <gen/QL/Legacy/Libormarketmodels/LmCorrelationModel.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (UInt64 size, Double rho, Double beta, Microsoft::FSharp::Core::FSharpOption<UInt64>^ factors) : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
    try
    {
#ifdef HANDLE
        _phLmLinearExponentialCorrelationModel = NULL;
#endif
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
        QuantLib::Real _rho = (QuantLib::Real)ValueHelper::Convert (rho);
        QuantLib::Real _beta = (QuantLib::Real)ValueHelper::Convert (beta);
        QuantLib::Size _factors = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (factors) ? (QuantLib::Size)ValueHelper::Convert (factors->Value) : Null<QuantLib::Size>()); //4
        _ppLmLinearExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel> (new QuantLib::LmLinearExponentialCorrelationModel ( _size,  _rho,  _beta,  _factors ));
        SetLmCorrelationModel (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel>& childNative, Object^ owner) : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialCorrelationModel = NULL;
#endif
	_ppLmLinearExponentialCorrelationModel = &childNative;
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (QuantLib::LmLinearExponentialCorrelationModel& childNative, Object^ owner) : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialCorrelationModel = NULL;
#endif
	_ppLmLinearExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel> (&childNative);
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
    _LmLinearExponentialCorrelationModelOwner = owner;
    _LmCorrelationModelOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (CLmLinearExponentialCorrelationModel^ copy) : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialCorrelationModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmLinearExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel> (copy->GetShared());
        _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (System::Type^ t) : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialCorrelationModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmLinearExponentialCorrelationModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (QuantLib::Handle<QuantLib::LmLinearExponentialCorrelationModel>& childNative, Object^ owner)  : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
	_phLmLinearExponentialCorrelationModel = &childNative;
	_ppLmLinearExponentialCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel>>(childNative.currentLink());
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
    _LmLinearExponentialCorrelationModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (QuantLib::Handle<QuantLib::LmLinearExponentialCorrelationModel> childNative)  : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
	_phLmLinearExponentialCorrelationModel = &childNative;
	_ppLmLinearExponentialCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel>>(childNative.currentLink());
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::CLmLinearExponentialCorrelationModel (QuantLib::LmLinearExponentialCorrelationModel childNative)  : CLmCorrelationModel(CLmLinearExponentialCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialCorrelationModel = NULL;
#endif
	_ppLmLinearExponentialCorrelationModel = new boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel> (new QuantLib::LmLinearExponentialCorrelationModel (childNative));
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmLinearExponentialCorrelationModel));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::~CLmLinearExponentialCorrelationModel ()
{
    if (_ppLmLinearExponentialCorrelationModel != NULL)
    {
	    delete _ppLmLinearExponentialCorrelationModel;
        _ppLmLinearExponentialCorrelationModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::!CLmLinearExponentialCorrelationModel ()
{
    if (_ppLmLinearExponentialCorrelationModel != NULL)
    {
	    delete _ppLmLinearExponentialCorrelationModel;
    }
}
QuantLib::LmLinearExponentialCorrelationModel& Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::GetReference ()
{
    if (_ppLmLinearExponentialCorrelationModel == NULL) throw gcnew NativeNullException ();
	return **_ppLmLinearExponentialCorrelationModel;
}
boost::shared_ptr<QuantLib::LmLinearExponentialCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::GetShared ()
{
    if (_ppLmLinearExponentialCorrelationModel == NULL) throw gcnew NativeNullException ();
	return *_ppLmLinearExponentialCorrelationModel;
}
QuantLib::LmLinearExponentialCorrelationModel* Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::GetPointer ()
{
    if (_ppLmLinearExponentialCorrelationModel == NULL) throw gcnew NativeNullException ();
	return &**_ppLmLinearExponentialCorrelationModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmLinearExponentialCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::GetHandle ()
{
	if (_phLmLinearExponentialCorrelationModel == NULL)
	{
		_phLmLinearExponentialCorrelationModel = new Handle<QuantLib::LmLinearExponentialCorrelationModel> (*_ppLmLinearExponentialCorrelationModel);
	}
	return *_phLmLinearExponentialCorrelationModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::HasNative () 
{
	return (_ppLmLinearExponentialCorrelationModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::Correlation (UInt64 i, UInt64 j, Double t, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmLinearExponentialCorrelationModel)->correlation ( _i,  _j,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLmLinearExponentialCorrelationModel)->factors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel::IsTimeIndependent::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppLmLinearExponentialCorrelationModel)->isTimeIndependent ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILmLinearExponentialCorrelationModel^ Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialCorrelationModel_Factory::Create (UInt64 size, Double rho, Double beta, Microsoft::FSharp::Core::FSharpOption<UInt64>^ factors)
{
    return gcnew CLmLinearExponentialCorrelationModel ( size,  rho,  beta,  factors);
}
